package com.qifenqian.turbot.module.adapter.itemdecoration

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.LinearLayout
import org.jetbrains.anko.dip

/**
 * @author H
 * @createTime 2018/12/26 14:48
 * @description RecyclerView的横向的分隔
 * @see
 * @since
 */

class LinearItemDecoration(context: Context, orientation: Int = VERTICAL, spaceWidth: Int = 16, color: Int = -1) : RecyclerView.ItemDecoration() {

    companion object {
        const val VERTICAL = 1
        const val HORIZONTAL = 0
    }

    private var mContext: Context? = null
    private var mOrientation: Int = LinearLayout.VERTICAL
    private var mSpaceWidth = -1

    private var mDividerDrawable: ColorDrawable? = null

    init {
        mContext = context
        mOrientation = orientation
        mSpaceWidth = mContext!!.dip(spaceWidth)
        mDividerDrawable = if (color == -1) null else ColorDrawable(color)
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        if (mOrientation == HORIZONTAL) {
            when (parent.getChildAdapterPosition(view)) {
                0                                    -> {
                    outRect.left = 0
                    outRect.right = mSpaceWidth / 2
                }
                parent.layoutManager!!.itemCount - 1 -> {
                    outRect.left = mSpaceWidth / 2
                    outRect.right = 0
                }
                else                                 -> {
                    outRect.left = mSpaceWidth / 2
                    outRect.right = mSpaceWidth / 2
                }
            }
        } else if (mOrientation == VERTICAL) {
            when (parent.getChildAdapterPosition(view)) {
                0                                    -> {
                    outRect.top = 0
                    outRect.bottom = mSpaceWidth / 2
                }
                parent.layoutManager!!.itemCount - 1 -> {
                    outRect.top = mSpaceWidth / 2
                    outRect.bottom = 0
                }
                else                                 -> {
                    outRect.top = mSpaceWidth / 2
                    outRect.bottom = mSpaceWidth / 2
                }
            }
        }
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        val layoutManager = parent.layoutManager as LinearLayoutManager?
        if (mDividerDrawable == null || layoutManager!!.childCount == 0) {
            return
        }
        var left: Int
        var right: Int
        var top: Int
        var bottom: Int
        val childCount = parent.childCount
        if (layoutManager.orientation == GridLayoutManager.VERTICAL) {
            for (i in 0 until childCount - 1) {
                val child = parent.getChildAt(i)
                val params = child.layoutParams as RecyclerView.LayoutParams
                //将有颜色的分割线处于中间位置
                val center = (layoutManager.getTopDecorationHeight(child) - mSpaceWidth / 2) / 2
                //计算下边的
                left = layoutManager.getLeftDecorationWidth(child)
                right = parent.width - layoutManager.getLeftDecorationWidth(child)
                top = (child.bottom.toFloat() + params.bottomMargin.toFloat() + center).toInt()
                bottom = top + mSpaceWidth / 2
                mDividerDrawable!!.setBounds(left, top, right, bottom)
                mDividerDrawable!!.draw(c)
            }
        } else {
            for (i in 0 until childCount - 1) {
                val child = parent.getChildAt(i)
                val params = child.layoutParams as RecyclerView.LayoutParams
                //将有颜色的分割线处于中间位置
                val center = (layoutManager.getLeftDecorationWidth(child) - mSpaceWidth / 2) / 2
                //计算右边的
                left = (child.right.toFloat() + params.rightMargin.toFloat() + center).toInt()
                right = left + mSpaceWidth / 2
                top = layoutManager.getTopDecorationHeight(child)
                bottom = parent.height - layoutManager.getTopDecorationHeight(child)
                mDividerDrawable!!.setBounds(left, top, right, bottom)
                mDividerDrawable!!.draw(c)
            }
        }
    }

}
